/**
 * Copyright (c) 2011 - 2012 National Library of Medicine
 *
 * Contact: tomasz.adamusiak@nih.gov;
 * 
 * This file is part of LOwlINC
 * 
 * LOwlINC is free software: you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option) any
 * later version.
 * 
 * LOwlINC is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License along
 * with LOwlINC. If not, see <http://www.gnu.org/licenses/>.
 */
package gov.nih.nlm.lowlinc.csv;

import gov.nih.nlm.lowlinc.accessionwrapper.LOINCID;
import gov.nih.nlm.lowlinc.accessionwrapper.LOINCPartID;
import gov.nih.nlm.lowlinc.bean.LOINCAxialBean;
import gov.nih.nlm.lowlinc.core.LowlincConfig;
import gov.nih.nlm.lowlinc.owlapi.LowlincException;

import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

import org.apache.log4j.Logger;

import au.com.bytecode.opencsv.CSVReader;

/**
 * @author Tomasz Adamusiak
 * 
 */
public class LOINCAxialCSVParser {
	private static final Logger log = Logger
			.getLogger(LOINCAxialCSVParser.class);
	private String[] headerCSV;
	private final AxialGraph graph = new AxialGraph();

	public AxialGraph getGraph() {
		return graph;
	}

	public LOINCAxialCSVParser() throws LowlincException {
		try {
			CSVReader reader = new CSVReader(new FileReader(new File(
					LowlincConfig.getString("LOINCAxialCSVParser.path")))); //$NON-NLS-1$
			headerCSV = reader.readNext();
			String[] nextLine;

			while ((nextLine = reader.readNext()) != null) {

				// parse child parent
				LOINCAxialBean bean = bindStringToBean(nextLine);
				LOINCID childID = bean.getChildLOINCID();
				if (bean.getIMMEDIATE_PARENT().equals("")) {
					continue;
				}
				LOINCPartID parentID = bean.getParentPartID();

				graph.addParentChild(parentID, childID);

			}

			// TODO: orphaned axials
			log.info(graph.getParts().size() + " axial parts loaded");
			log.info(graph.getTerms().size() + " axial terms loaded");

		} catch (IOException e) {
			throw new LowlincException(e);
		}
	}

	private LOINCAxialBean bindStringToBean(String[] line)
			throws LowlincException {
		try {
			LOINCAxialBean bean = new LOINCAxialBean();

			for (int i = 0; i < headerCSV.length; i++) {
				Method meth = bean.getClass().getMethod("set" + headerCSV[i],
						String.class);
				meth.invoke(bean, line[i]);
			}
			return bean;

		} catch (NoSuchMethodException e) {
			throw new LowlincException(e);
		} catch (SecurityException e) {
			throw new LowlincException(e);
		} catch (IllegalAccessException e) {
			throw new LowlincException(e);
		} catch (IllegalArgumentException e) {
			throw new LowlincException(e);
		} catch (InvocationTargetException e) {
			throw new LowlincException(e);
		}
	}
}
